package com.example.sunsg.okhttptest.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class MiddleLightView extends View {
    private int mW;
    private int mH;
    private Paint mPaint;
    private Rect mRect;
    private Rect mRect1;
    public MiddleLightView(Context context) {
        super(context);
        init();
    }

    public MiddleLightView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MiddleLightView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.parseColor("#AF000000"));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mW = w;
        mH = h;
        if(mRect == null){
            mRect = new Rect(0,0,mW,mH);
        }

        if(mRect1 == null){
            mRect1 = new Rect(mW/4,mH/4, (int) (mW*3.0f/4), (int) (mH*3.0f/4));
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int layid = canvas.saveLayer(0, 0, mW, mH, mPaint, Canvas.ALL_SAVE_FLAG);
        mPaint.setColor(Color.parseColor("#AF000000"));
        canvas.drawRect(mRect,mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        mPaint.setColor(Color.parseColor("#FFFFFF00"));
        canvas.drawRect(mRect1,mPaint);
        mPaint.setXfermode(null);
        canvas.restoreToCount(layid);
    }
}
